home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / cpptutor.arc / CHAP02.TXT < prev    next >
Text File  |  1991-04-28  |  10KB  |  236 lines

  1.  
  2.  
  3.  
  4.                                                         Chapter 2
  5.                                                    COMPOUND TYPES
  6.  
  7.  
  8. ENUMERATED TYPES
  9. _________________________________________________________________
  10.  
  11. Examine the file named ENUM.CPP for an example     ==============
  12. that uses an enumerated type variable.  The           ENUM.CPP
  13. enumerated type is used in C++ in exactly the      ==============
  14. same way it was used in ANSI-C with one small
  15. exception, the keyword enum is not required to
  16. be used again when defining a variable of that type, but it can be
  17. used if desired.  It may be clearer to you to use the keyword again
  18. in the same manner that it is required to be used in C.
  19.  
  20. The example program uses the keyword enum in line 9, but omits it
  21. in line 8 to illustrate to you that it is indeed optional.  The
  22. remainder of this program should be no problem for you to
  23. understand.  After studying it, be sure to compile and execute it
  24. and examine the output.
  25.  
  26.  
  27. A SIMPLE STRUCTURE
  28. _________________________________________________________________
  29.  
  30. Examine the example program named STRUCTUR.CPP   ================
  31. for an illustration which uses a very simple       STRUCTUR.CPP
  32. structure.  This structure is no different from  ================
  33. that used in ANSI-C except for the fact that the
  34. keyword struct is not required to be used again
  35. when defining a variable of that type.  Lines 11 and 12 illustrate
  36. the declaration of variables without the keyword, and line 13
  37. indicates that the keyword struct can be included if desired.  It
  38. is up to you to choose which style you prefer to use in your C++
  39. programs.
  40.  
  41. You should take note of the fact that this is a valid ANSI-C
  42. program except for the fact that it uses the stream library, the
  43. C++ comments, and the lack of use of the keyword struct in two of
  44. the lines.
  45.  
  46. Once again, be sure to compile and execute this program after
  47. studying it carefully, because the next example program is very
  48. similar but it introduces a brand new construct not available in
  49. standard C, the class.
  50.  
  51.  
  52. A VERY SIMPLE CLASS
  53. _________________________________________________________________
  54.  
  55. Examine the example program named CLASS1.CPP for our first example
  56. of a class in C++.  This is the first class example, but it will
  57.  
  58.                                                           Page 2-1
  59.  
  60.                                         Chapter 2 - Compound types
  61.  
  62. not be the last, since the class is the major     ================
  63. reason for using C++ over ANSI-C or some other       CLASS1.CPP
  64. programming language.  You will notice the        ================
  65. keyword class used in line 4, in very much the
  66. same way that the keyword struct was used in the
  67. last program, and they are in fact very similar constructs.  There
  68. are definite differences, as we will see, but for the present time
  69. we will be concerned more with their similarities.
  70.  
  71. The word animal in line 4 is the name of the class, and when we
  72. declare variables of this type in lines 12 through 14, we can
  73. either omit the keyword class or include it if we desire as
  74. illustrated in line 14.  In the last program, we declared 5
  75. variables of a structure type, but in this program we declare 5
  76. objects.  They are called objects because they are of a class type. 
  77. The differences are subtle, and in this case the differences are
  78. negligible, but as we proceed through this tutorial, we will see
  79. that the class construct is indeed very important and valuable. 
  80. The class was introduced here only to give you a glimpse of what
  81. is to come later in this tutorial.
  82.  
  83. The class is a type which can be used to declare objects in much
  84. the same way that a structure is a type that can be used to declare
  85. variables.  Your dog named King is a specific instance of the
  86. general class of dogs, and in a similar manner, an object is a
  87. specific instance of a class.  It would be well to take note of the
  88. fact that the class is such a generalized concept that there will
  89. be libraries of prewritten classes available in the marketplace
  90. soon.  You will be able to purchase classes which will perform some
  91. generalized operations such as managing stacks, queues, or lists,
  92. sorting data, managing windows, etc.  This is because of the
  93. generality and flexibility of the class construct.  In fact, a few
  94. class libraries are available now.
  95.  
  96. The new keyword public in line 5, followed by a colon, is necessary
  97. in this case because the variables in a class are defaulted to a
  98. private type and we could not access them at all without making
  99. them public.  Don't worry about this program yet, we will cover all
  100. of this in great detail later in this tutorial.
  101.  
  102. Be sure to compile and run it to see that it does what we say it
  103. does with your compiler.  Keep in mind that this is your first
  104. example of a class and it illustrates essentially nothing
  105. concerning the use of this powerful structure.
  106.  
  107.  
  108. THE FREE UNION OF C++
  109. _________________________________________________________________
  110.  
  111. Examine the program named UNIONEX.CPP for an      ===============
  112. example of a free union.  In ANSI-C, all unions     UNIONEX.CPP
  113. must be named in order to be used, but this is    ===============
  114. not true in C++.  We have the so called free
  115. union, the union without a name.  The union is
  116.                                                           Page 2-2
  117.  
  118.                                         Chapter 2 - Compound types
  119.  
  120. embedded within a simple structure and you will notice that there
  121. is not a variable name following the declaration of the union in
  122. line 11.  In ANSI-C, we would have to name the union and give a
  123. triple name (three names dotted together) to access the members. 
  124. Since it is a free union, there is no union name, and the variables
  125. are accessed with only a doubly dotted name as illustrated in lines
  126. 18, 22, 26, 28, and 29.
  127.  
  128. You will recall that a union causes all the data contained within
  129. the union to be stored in the same physical memory locations, such
  130. that only one variable is actually available at a time.  This is
  131. exactly what is happening here.  The variable named fuel_load,
  132. bomb_load, and pallets are stored in the same physical memory
  133. locations and it is up to the programmer to keep track of which is
  134. stored there.  You will notice that the transport is assigned a
  135. value for pallets in line 26, then a value for fuel_load in line
  136. 28.  When the value for fuel_load is assigned, the value for
  137. pallets is corrupted and is no longer available since it was stored
  138. where fuel_load is stored now.  The observant student will notice
  139. that this is exactly the way the union is used in ANSI-C except for
  140. the way components are named.
  141.  
  142. The remainder of the program should be easy for you to understand,
  143. so after you study it and understand it, compile and execute it.
  144.  
  145.  
  146. C++ TYPE CONVERSIONS
  147. _________________________________________________________________
  148.  
  149. Examine the program named TYPECONV.CPP for a few ================
  150. examples of type conversions in C++.  The type     TYPECONV.CPP
  151. conversions are done in C++ in exactly the same  ================
  152. manner as they are done in ANSI-C, but C++ gives
  153. you another form for doing the conversions.
  154.  
  155. Lines 10 through 18 of this program use the familiar "cast" form
  156. of type conversions used in ANSI-C, and there is nothing new to the
  157. experienced C programmer.  You will notice that lines 10 through
  158. 13 are actually all identical to each other.  The only difference
  159. is that we are coercing the compiler to do the indicated type
  160. conversions prior to doing the addition and the assignment in some
  161. of the statements.  In line 13, the int type variable will be
  162. converted to type float prior to the addition, then the resulting
  163. float will be converted to type char prior to being assigned to the
  164. variable c.
  165.  
  166. Additional examples of type coercion are given in lines 15 through
  167. 18 and all four of these lines are essentially the same.
  168.  
  169. The examples given in lines 20 through 28 are unique to C++ because
  170. they are not valid in ANSI-C.  In these lines the type coercions
  171. are written as though they are function calls instead of the "cast"
  172. method as illustrated earlier.  Lines 20 through 28 are identical
  173. to lines 10 through 18.
  174.  
  175.                                                           Page 2-3
  176.  
  177.                                         Chapter 2 - Compound types
  178.  
  179.  
  180. You may find this method of type coercion to be clearer and easier
  181. to understand than the "cast" method and in C++ you are free to use
  182. either, or to mix them if you so desire, but your code could be
  183. very difficult to read if you indescriminantly mix them.
  184.  
  185. Be sure to compile and execute this example program.
  186.  
  187.  
  188. PROGRAMMING EXERCISES
  189. _________________________________________________________________
  190.  
  191.  
  192. 1.   Starting with the program ENUM.CPP, add the enumerated value
  193.      of forfeit to the enumerated type game_result, and add a
  194.      suitable message and logic to get the message printed in some
  195.      way.
  196.  
  197. 2.   Add the variable number_of_ways to the class of CLASS1.CPP and
  198.      store some values in the new variable.  Print some of the
  199.      values out.  Move the new variable ahead of the keyword
  200.      public: and see what kind of error message results.  We will
  201.      cover this error in chapter 5 of this tutorial.
  202.  
  203.  
  204.  
  205.  
  206.  
  207.  
  208.  
  209.  
  210.  
  211.  
  212.  
  213.  
  214.  
  215.  
  216.  
  217.  
  218.  
  219.  
  220.  
  221.  
  222.  
  223.  
  224.  
  225.  
  226.  
  227.  
  228.  
  229.  
  230.  
  231.  
  232.  
  233.  
  234.                                                           Page 2-4
  235.  
  236.